package com.microblog.microblogsystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.microblog.microblogsystem.context.BaseContext;
import com.microblog.microblogsystem.domain.dto.BlogDTO;
import com.microblog.microblogsystem.domain.dto.BlogPageQueryDTO;
import com.microblog.microblogsystem.domain.dto.CommentDTO;
import com.microblog.microblogsystem.domain.po.Blog;
import com.microblog.microblogsystem.domain.po.Comment;
import com.microblog.microblogsystem.domain.po.Likes;
import com.microblog.microblogsystem.domain.vo.BlogReadVO;
import com.microblog.microblogsystem.result.PageResult;
import com.microblog.microblogsystem.result.Result;
import com.microblog.microblogsystem.service.BlogService;
import com.microblog.microblogsystem.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itlxc
 * @since 2024-06-28
 */
@RestController
@RequestMapping("/user/blog")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "博客相关接口")
public class BlogController {

    private final BlogService blogService;

    private final AliOssUtil aliOssUtil;

    /**
     * 分页查询博客信息
     * @param blogPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询博客信息")
    public Result<PageResult> pageQuery(BlogPageQueryDTO blogPageQueryDTO) {
        log.info("分页查询博客信息：{}", blogPageQueryDTO);
        return Result.success(blogService.pageQuery(blogPageQueryDTO));
    }

    /**
     * 获取博客评论数
     * @param bid
     * @return
     */
    @GetMapping("/commentCount")
    @ApiOperation(value = "获取博客评论数")
    public Result<Long> getCommentCount(Long bid) {
        log.info("获取博客评论数，当前博客文章id：{}", bid);
        Long count = Db.lambdaQuery(Comment.class)
                       .eq(Comment::getBid, bid)
                       .count();
        return Result.success(count);
    }

    /**
     * 获取博客点赞数
     * @param bid
     * @return
     */
    @GetMapping("/likeCount")
    @ApiOperation(value = "获取博客点赞数")
    public Result<Long> getLikeCount(Long bid) {
        log.info("获取博客点赞数，当前博客文章id：{}", bid);
        Long count = Db.lambdaQuery(Likes.class)
                       .eq(Likes::getBid, bid)
                       .count();
        return Result.success(count);
    }

    /**
     * 用户点赞博客
     * @param bid
     * @return
     */
    @PutMapping("/like")
    @ApiOperation(value = "用户点赞博客")
    public Result like(Long bid) {
        log.info("用户点赞博客，当前博客的id：{}", bid);
        blogService.like(bid);
        return Result.success();
    }

    /**
     * 阅读博客
     * @param bid
     * @return
     */
    @GetMapping("/read/{bid}")
    @ApiOperation(value = "阅读博客文章")
    public Result<BlogReadVO> read(@PathVariable("bid") Long bid) {
        log.info("阅读博客文章，当前博客文章的id：{}", bid);
        return Result.success(blogService.read(bid));
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传图片")
    public Result<String> upload(MultipartFile file) {
        log.info("上传图片：{}", file.getOriginalFilename());
        try {
            String url = aliOssUtil.upload(file);
            log.info("上传图片成功，图片地址为：{}", url);
            return Result.success(url);
        } catch (IOException e) {
            log.error("上传图片失败，异常信息为：{}", e.getMessage());
            return Result.error("上传图片失败");
        }
    }

    /**
     * 发布博客文章
     * @param blogDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "发布博客文章")
    public Result saveBlog(@RequestBody BlogDTO blogDTO) {
        log.info("发布博客文章：{}", blogDTO);
        Blog blog = BeanUtil.copyProperties(blogDTO, Blog.class);
        blog.setTime(LocalDateTime.now());
        blog.setUid(BaseContext.getCurrentId());
        blogService.save(blog);
        return Result.success();
    }

    /**
     * 发布评论
     * @param commentDTO
     * @return
     */
    @PostMapping("/addComment")
    @ApiOperation(value = "发布评论")
    public Result saveComment(@RequestBody CommentDTO commentDTO) {
        log.info("发布评论：{}", commentDTO);
        Comment comment = BeanUtil.copyProperties(commentDTO, Comment.class);
        comment.setUid(BaseContext.getCurrentId());
        comment.setTime(LocalDateTime.now());
        Db.save(comment);
        return Result.success();
    }
}
